﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.Core.Actions
{
    public class VoBoEditSourceDocumentCommand : IBaseCommand
    {

        #region fields

        int _selectedItem;
        Guid _sourceLibraryId;
        string _notes;
        string _targetUser;
        TaxonomyFieldValue _currentCategoryValue;
        string _currentUserAssigned;
        string _currentArchiveRequest;
        string _currentArchiveResponse;
        SPWeb _currentWeb;

        #endregion

        #region Constructors

        public VoBoEditSourceDocumentCommand(int selectedItem, Guid sourceLibrary, string notes, string targetUser, SPWeb currentWeb)
        {
            _selectedItem = selectedItem;
            _sourceLibraryId = sourceLibrary;
            _notes = notes;
            _targetUser = targetUser;
            _currentWeb = currentWeb;
        }

        #endregion

        public void Execute()
        {

            try
            {
                SPList sourceLibrary = _currentWeb.Lists[_sourceLibraryId];
                SPFile sourceFile = sourceLibrary.GetItemById(_selectedItem).File;
                if (sourceFile == null || (sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] == null || ((TaxonomyFieldValue)sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY]).Label != GlobalConstants.METADATA_TERM_CATEGORY_VOBO))
                    editSourceDocument(sourceFile, _targetUser);
            }
            catch (Exception ex)
            {
                Undo();
                throw ex;
            }

        }

        public void Undo()
        {
            SPList sourceLibrary = _currentWeb.Lists[_sourceLibraryId];

            SPFile sourceFile = sourceLibrary.GetItemById(_selectedItem).File;
            sourceFile.Item[CustomSiteColumns.FIELD_ARCHIVE_ASSIGNED] = _currentUserAssigned;
            sourceFile.Item[CustomSiteColumns.FIELD_ARCHIVE_REQUEST] = _currentArchiveRequest;
            sourceFile.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] = _currentCategoryValue;
            sourceFile.Item.Update();
        }

        #region private methods

        private void editSourceDocument(SPFile sourceDocument, string loginName)
        {

            //Saving current values

            _currentUserAssigned = sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_ASSIGNED] == null ? string.Empty : sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_ASSIGNED].ToString();
            _currentArchiveRequest = sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_REQUEST] == null ? string.Empty : sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_REQUEST].ToString();
            _currentArchiveResponse = sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_RESPONSE] == null ? string.Empty : sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_RESPONSE].ToString();
            _currentCategoryValue = sourceDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] as TaxonomyFieldValue;

            SPFieldUserValue targetUser = PeopleHelper.GetUserByLoginName(loginName);
            sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_ASSIGNED] = targetUser;
            sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_REQUEST] = _notes;
            sourceDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_CATEGORY, GlobalConstants.METADATA_TERM_CATEGORY_VOBO);
            sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_RESPONSE] = string.Empty;
            sourceDocument.Item[CustomSiteColumns.FIELD_ARCHIVE_APPROVAL] = null;
            sourceDocument.Log(string.Empty, DateTime.Now, InformationManagement.Support.Enums.TraceableOperation.Revisión, SPContext.Current.Web.CurrentUser.Name, _notes);
            sourceDocument.Item.Update();
        }

        #endregion
    }
}
